var enterprise;

var prdEquManagementCookieNum=1;


var label = ['order', 'sn', 'name',  'ceqNumber', 'createTime','maintenanceCycle', 'opera'];
var title = ['#', '设备编号', '设备名称', '采集设备编号','添加时间', '维保周期', '操作'];

$(function () {
// enterprise= sessionStorage.getItem("enterprise");
    enterprise = 1;
    if($.cookie("prdEquManagementCookieNum")){
        prdEquManagementCookieNum=$.cookie("prdEquManagementCookieNum");
        loadDeviceList(prdEquManagementCookieNum);
    }else{
        loadDeviceList();
    }
    userInfo($('#userInfo'));
})

/*加载设备列表*/
function loadDeviceList(_pageIndex) {
    showLoading(LOADING);
    var pageIndex = parseInt(_pageIndex ? _pageIndex : '1');
    getJson("/deviceData/getDeviceListByEnterprise", {
        "param": $('#param_id').val(),
        pageIndex: pageIndex,
        pageSize: 10
    }, 'GET', function (data) {
        var payload = data['payload'];
        var order = 1 + 10 * (pageIndex - 1);
        payload.data.map(function (item) {
            item['order'] = order++;
            item['createTime']=getDate(item['createTime']).datetime();
        });
        buildTable('#device_List', title, label, payload['data'], function (td, label, item) {
            switch (label) {
                case "order":
                    $(td).parent().attr('id', 'tr_' + item['order']);
                    break;
                case 'opera':
                    var info = createElement('a', td, '详情');
                    $(info).addClass('more-info');
                    $(info).attr('href', '#');
                    $(info).click(function () {
                        viewMore(item['id']);
                    });
                    break
            }
        });
        setPage($('#page'), pageIndex, payload['pages'], payload['total'], function (num) {
            $.cookie("prdEquManagementCookieNum",num,{path: '/'});
            loadDeviceList(num);
        });
        hideLoading();
    })
}

/*查看详情*/
function viewMore(e) {
    /*根据ID查询信息*/
    getJson("/deviceData/getProduceDeviceById",{"id": e},'GET',function (result) {
        if (result.status == 200) {
            var data = result.data;
            $('#name-id').text(data.name);
            $('#sn-id').text(data.sn);
            $('#ceqNumber-id').text(data.ceqNumber);
            $('#port-id').text(data.port);
            $('#maintenanceCycle-id').text(data.maintenanceCycle + "天");
            $('#minPrice-id').text(data.minPrice + '元/kWh');
            $('#maxPrice-id').text(data.maxPrice + '元/kWh');
            $('#avgPrice-id').text(data.avgPrice + '元/kWh');
            $('#maxU-id').text(data.maxU + 'V');
            $('#maxI-id').text(data.maxI + 'A');
            $('#maxC-id').text(data.maxC + 'kWh');
            $('#standardU-id').text(data.standardU + 'V');
            $('#standardI-id').text(data.standardI + 'A');
            $('#standardC-id').text(data.standardC + 'kWh');
            $('#standardPower-id').text(data.standardPower + "kW");
            $('#qddl_id').text(data.iah + "A");
            $('#djdl_id').text(data.ial + "A");
            if(!isNull(data.image)){
                $('#logo-id').attr('src', fileUrl + data.image);
                $('#logo-id')[0].onerror = function () {//加载出错时，替换为默认图
                    $(this).attr('src', '../customize/images/device-img.png' );
                }
            }else{//为空时，替换为默认图
                $('#logo-id').attr('src', '../customize/images/device-img.png' );
            }
        } else {
            myAlert("提示", "加载设备数据失败");
        }
    })
    $('#viewInfoModal').modal();
}